package com.hnchances.grade.service.impl;

import com.hnchances.grade.common.utils.JWTUtils;
import com.hnchances.grade.common.utils.R;
import com.hnchances.grade.entity.*;
import com.hnchances.grade.service.*;
import com.hnchances.grade.vo.GradesVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnchances.grade.common.utils.PageUtils;
import com.hnchances.grade.common.utils.Query;

import com.hnchances.grade.dao.GradesDao;


@Service("gradesService")
public class GradesServiceImpl extends ServiceImpl<GradesDao, GradesEntity> implements GradesService {

    @Autowired
    private ClassService classService;
    @Autowired
    private ExamService examService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private UserService userService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GradesEntity> page = this.page(
                new Query<GradesEntity>().getPage(params),
                new QueryWrapper<GradesEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 转换前端传来的数据并进行保存
     * name->id
     *
     * @param gradesVos
     * @return
     */
    @Override
    public R saveDetails(List<GradesVo> gradesVos) {
        /**
         * 实际业务中，对于录入成绩肯定是对于本班学生的成绩进行录入
         * 故而 每条记录中只有学生id和成绩不一致，其余都一致；
         */
        //取出一项查出公共数据
        if (gradesVos.size() == 0 || gradesVos == null) {
            return R.error("至少修改一条数据！");
        }
        GradesVo gradesVo = gradesVos.get(0);
        //公共数据的查询与安全性判断
        String courseName = gradesVo.getCourseName();
        CourseEntity course = courseService.getOne(
                new QueryWrapper<CourseEntity>().eq("courseName", courseName));
        if (course == null) {
            return R.error("请输入正确的课程名称！");
        }
        String examName = gradesVo.getExamName();
        ExamEntity exam = examService.getOne(
                new QueryWrapper<ExamEntity>().eq("examName", examName));
        if (exam == null) {
            return R.error("请输入正确的考试名称！");
        }
        String className = gradesVo.getClassName();
        ClassEntity classEntity = classService.getOne(
                new QueryWrapper<ClassEntity>().eq("className", className));
        if (classEntity == null) {
            return R.error("请输入正确的班级名称！");
        }
        List<GradesEntity> collect = gradesVos.stream().map(item -> {
            GradesEntity gradesEntity = new GradesEntity();
            BeanUtils.copyProperties(item, gradesEntity);
            gradesEntity.setExamId(exam.getId());
            gradesEntity.setClassId(classEntity.getId());
            gradesEntity.setCourseId(course.getId());
            gradesEntity.setStatus(1);
            return gradesEntity;
        }).collect(Collectors.toList());
        this.saveBatch(collect);
        return R.ok();
    }

    /**
     * 多条件查询
     * @param params
     * @param token
     * @return
     */
    @Override
    public R queryPageByCondition(Map<String, Object> params, String token) {
        //获取到当前老师所带的班级
        String username = JWTUtils.getUsername(token);
        UserEntity user = userService.getOne(new QueryWrapper<UserEntity>().eq("userName", username));
        Integer teacherClassId = user.getClassId();
        //获取到查询条件的参数
        //考虑到学生重名，所以返回的数据都是list
        //学生姓名查询
        QueryWrapper<GradesEntity> wrapper = new QueryWrapper<>();
        String studentName = (String) params.get("studentName");
        if (studentName != null && studentName != ""){
            StudentEntity student = studentService.getOne(new QueryWrapper<StudentEntity>().eq("name", studentName));
            Integer id = student.getId();
            if (studentName != null && studentName != "") {
                wrapper.eq("studentID", id);
            }
        }
        //班级名称查询
        String className = (String) params.get("className");

        if (className != null) {
            ClassEntity classEntity = classService.getOne(new QueryWrapper<ClassEntity>().eq("className", className));
            Integer id1 = classEntity.getId();
            if (id1 != teacherClassId) {
                return R.error("您只能查看本班级学生的成绩");
            }
            wrapper.eq("classID", id1);
        }
        //课程名称查询
        String courseName = (String) params.get("courseName");

        if (courseName != null && courseName != "") {
            CourseEntity course = courseService.getOne(new QueryWrapper<CourseEntity>().eq("courseName", courseName));
            Integer id2 = course.getId();
            wrapper.eq("courseID", id2);
        }
        //考试名称查询
        String examName = (String) params.get("examName");
        if (examName != null && examName != "") {
            ExamEntity exam = examService.getOne(new QueryWrapper<ExamEntity>().eq("examName", examName));
            Integer id1 = exam.getId();
            wrapper.eq("examID", id1);
        }
        IPage<GradesEntity> page = this.page(
                new Query<GradesEntity>().getPage(params),
                wrapper
        );
        return R.ok().put("page", page);
    }

}